﻿/*
 * 访问统计代码，放入bottom.html
<script language="javascript">
    var purl = document.referrer;
    document.write("<script language=\"javascript\" src=\"../Statistic.aspx?purl="+purl+"\"><\/script>");
</script>
 
 */

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Whir.Common.WebHelper;

public partial class Statistic : System.Web.UI.Page
{
    public string purl = "";

    protected void Page_Load(object sender, EventArgs e)
    {
        purl = NetRequest.GetQueryString("purl");
        if (!IsPostBack)
        {
            if (string.IsNullOrEmpty(purl))
                return;
            StatAdd();
        }
    }

    //访问统计
    protected void StatAdd()
    {
        if (!IsAdd1() || !IsAdd2())
            return;

        Whir.Model.Whir_AccessStat model = new Whir.Model.Whir_AccessStat();

        //获取客户端IP
        model.IP = Request.UserHostAddress;

        //获取来源网址
        model.originUrl = purl;

        //获取客户端操作系统
        model.OS = Request.Browser.Platform;

        //获取客户端浏览器信息
        model.browser = Request.Browser.Browser + " " + Request.Browser.Version;

        //访问页面
        model.accessUrl = Request.UrlReferrer.ToString();

        //访问时间
        model.createDate = DateTime.Now;

        new Whir.BLL.Whir_AccessStat().Add(model);
    }

    //来源域名判断
    protected bool IsAdd1()
    {
        //判断是否来源于自身网站
        string[] url1 = purl.Replace("http://", "").Split('/');
        string[] url2 = Request.Url.ToString().ToLower().Replace("http://", "").Split('/');

        if (url1[0] != url2[0])    //本站内页面跳转不统计
            return true;
        else
            return false;
    }

    //IP访问时间判断
    protected bool IsAdd2()
    {
        DataTable dt = Whir.SQLServerDAL.DbHelperSQL.GetDataTable("top 1 datediff(minute,createDate,getdate())", "Whir_AccessStat", "IP='" + Request.UserHostAddress + "'", "createDate desc");

        int m = 0;

        if (dt.Rows.Count == 0)
            return true;
        else
            m = int.Parse(dt.Rows[0][0].ToString());

        if (m >= 20)     //20分钟内重复访问不统计
            return true;
        else
            return false;
    }
}